<?php

class Entidade_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {

    }

    public function classeAction()
    {

      $listaTabela = $this->getRequest()->getParam( 'no_tabela' );
      
      if( strpos( $listaTabela, "," ) > 0 ){
        
        $aNoTabela = explode( ',', $listaTabela );
      }else{
        
        $aNoTabela = array( 'nome_classe' => $listaTabela );
      }
      
      $arrClasse = array();
      
      foreach( $aNoTabela as $noTabela ){
                
        $gerador = new Entidade_Model_GeradorClasse();
        
        $arrClasse[] = array(           
          'nome_classe' => $noTabela,
          'corpo_entity' => $gerador->getEntity( trim( $noTabela ) )->generate(), 
          'corpo_model' => $gerador->getModel( trim( $noTabela ) )->generate(), 
          'corpo_repository' => $gerador->getRepository( trim( $noTabela ) )->generate()
        );
      }

      $this->view->message = '';
      
      foreach( $arrClasse as $chave => $classe ){
        
        if (!is_dir(APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/")) {
          
          mkdir(APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/");
          mkdir(APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/models/");
          mkdir(APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/models/entity/");
          mkdir(APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/models/entity/repository/");              
        }
        
        foreach ($classe as $key => $value) {
          
          switch ($key) {
            
            case 'corpo_model':
              
              
              
              $fl = "<?php \n\nnamespace " . Zend_Registry::get('config')->db->module . "\\models\\entity; \n\n"; 
              $fl .= "\nuse " . Zend_Registry::get('config')->db->module . "\\models as Models, " . Zend_Registry::get('config')->db->module . "\\models\\entity as Entity;\n\n";         
              $fl .= $value;

              $flx = fopen( APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/models/" . $classe['nome_classe'] . "Model.php", 'w+');
              fwrite( $flx, $fl );
              fclose($flx);
              break;
            
            case 'corpo_entity':

              $fl = "<?php \n\nnamespace " . Zend_Registry::get('config')->db->module . "\\models\\entity; \n\n"; 
              $fl .= "\nuse " . Zend_Registry::get('config')->db->module . "\\models as Models, " . Zend_Registry::get('config')->db->module . "\\models\\entity as Entity;\n\n";         
              $fl .= $value;
              
              $flx = fopen( APPLICATION_PATH . "/../public/arquivos/". $classe['nome_classe'] . "/models/entity/" . $classe['nome_classe'] . ".php", 'w+');
              fwrite( $flx, $fl );
              fclose($flx);
              break;            

            case 'corpo_repository':
              
              $fl = "<?php \n\nnamespace " . Zend_Registry::get('config')->db->module . "\\models\\entity\\repository; \n\n"; 
              $fl .= "\nuse Doctrine\\ORM\\EntityRepository, Doctrine\ORM\Tools\Pagination\Paginator;\n\n";         
              $fl .= $classe['corpo_repository'];
              
              $flx = fopen( APPLICATION_PATH . "/../public/arquivos/" . $classe['nome_classe'] . "/models/entity/repository/". $classe['nome_classe'] . "Repository.php", 'w+');
              fwrite( $flx, $fl );
              fclose($flx);
              break;
          }        
        }
        
        $this->view->message .= 'Classe "' . $classe['nome_classe'] . '" criada na pasta "arquivos" com sucesso.<br>';
      }
    }
}

